<template>
  <div class="home">
    <el-container style="height: 85vh; ">
      <menu-home @menuClick="selectIndex" />
      <el-container>
        <content-home
          v-if="showTable"
          :tableData="currentTable"
          :isEmp="isEmp"
          :tableName="currentTableName"
        />
        <insert-table v-else :titles="titles" :tableName="currentTableName" />
      </el-container>
    </el-container>
  </div>
</template>

<script>
import MenuHome from './childComps/MenuHome'
import ContentHome from './childComps/ContentHome'
import FooterHome from './childComps/FooterHome'
import InsertTable from 'components/content/InsertTable'

import { getHomeData, getStatistics } from 'network/home'
export default {
  name: 'Home',
  components: {
    MenuHome,
    ContentHome,
    FooterHome,
    InsertTable
  },

  data() {
    return {
      isLoading: false,
      tableData: [],
      tableTitle: [
        'Employee',
        'Dept',
        'Contract',
        'Train',
        'Rewards',
        'Job',
        'Titles'
      ],
      showTable: true,
      currentIndex: 1
    }
  },

  created() {
    /**
     * 请求数据
     */

    //1.请求主页数据

    this._getHomeDate()

    //2.请求统计数据
    getStatistics().then(res => {
      // this.option = new Option(res.data)
      // console.log(option)
      this.$store.dispatch('addStatisticsTo', res.data)
    })
  },

  computed: {
    currentTableName() {
      return this.tableTitle[this.currentIndex - 1]
    },

    currentTable() {
      return (
        this.tableData !== undefined && this.tableData[this.currentTableName]
      )
    },

    isEmp() {
      return this.currentIndex == 1
    },

    titles() {
      return this.currentTable.length !== 0 && Object.keys(this.currentTable[0])
    }
  },

  methods: {
    /**
     * 事件响应
     */
    selectIndex(info) {
      this.currentIndex = info.index
      if (info.type == '1') {
        this.showTable = true
      } else if (info.type == '2') {
        this.showTable = false
      }
      if (info.index == 3) {
        this.overTime()
      }
    },

    overTime() {
      //合同到期提醒函数
      let date_now = new Date().getTime()
      this.currentTable.forEach(item => {
        let old_date = new Date(item.duration).getTime()
        let diff = old_date - date_now
        let days = parseInt(diff / (1000 * 60 * 60 * 24))
        if (days <= 30 && days >= 0) {
          this.$message({
            message:
              '合同ID' +
              item.c_id +
              ': ' +
              item.type +
              '还有' +
              days +
              '天到期',
            type: 'warning'
          })
        } else if (days < 0) {
          this.$message.error(
            '合同ID' + item.c_id + ': ' + item.type + '已经到期' + -days + '天'
          )
        }
      })
    },

    /**
     * 请求数据
     */
    _getHomeDate() {
      getHomeData().then(res => {
        console.log(res.data)

        this.tableData = res.data
      })
    }
  }
}
</script>

<style scoped></style>
